#对验证码进行识别处理
#先将验证码流程进行本地下载，在使用超级鹰进行破解上传
from lxml import etree
import requests

from CodeMethod.chaojiying import Chaojiying_Client


#封装一个识别函数
def getCodeText(path):
    result = None
    chaojiying = Chaojiying_Client('15178530907', '340223wys', '952786')
    im = open(path, 'rb').read()
    result = chaojiying.PostPic(im, 1902)["pic_str"]
    print(result)
    return result



#1.将验证码图片下载到本地


if __name__=='__main__':
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)'
                      ' Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.76'
    }
    url = 'https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx'
    page_text = requests.get(url=url,headers=headers).text
    #解析验证码图片
    tree = etree.HTML(page_text)
    code_src = 'https://so.gushiwen.cn/'+tree.xpath('//*[@id="imgCode"]/@src')[0]
    print(code_src)
    img_data = requests.get(url=code_src,headers=headers).content
    with open('./code.jpg','wb') as fp:
        fp.write(img_data)

#调用超级应平台进行识别

    codo_text = getCodeText('./code.jpg')
    print(codo_text)